package com.qin.malldiscount.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qin.malldiscount.dao.SmsFlashPromotionProductRelationDao;
import com.qin.malldiscount.domain.SmsFlashPromotionProductRelation;
import com.qin.malldiscount.dto.SmsFlashPromotionProduct;
import com.qin.malldiscount.mapper.PromotionProductRelationMapper;
import com.qin.malldiscount.service.SmsFlashPromotionProductRelationService;
import com.qin.malldiscount.mapper.SmsFlashPromotionProductRelationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 覃剑
* @description 针对表【sms_flash_promotion_product_relation(商品限时购与商品关系表)】的数据库操作Service实现
* @createDate 2022-11-23 19:34:44
*/
@Service
public class SmsFlashPromotionProductRelationServiceImpl extends ServiceImpl<SmsFlashPromotionProductRelationMapper, SmsFlashPromotionProductRelation>
    implements SmsFlashPromotionProductRelationService{

    @Autowired
    private SmsFlashPromotionProductRelationMapper relationMapper;
    @Autowired
    private SmsFlashPromotionProductRelationDao relationDao;
    @Autowired
    private PromotionProductRelationMapper promotionProductRelationMapper;

    /**
     * 批量添加关联
     */
    @Override
    public int create(List<SmsFlashPromotionProductRelation> relationList) {
        //对呀循环插入可以使用这种方法
        for (SmsFlashPromotionProductRelation relation : relationList) {
            int count = relationMapper.insert(relation);
        }
        return relationList.size();
    }

    @Override
    public int update(Long id, SmsFlashPromotionProductRelation relation) {
        relation.setId(id);
        int count = relationMapper.updateById(relation);
        return count;
    }

    @Override
    public int delete(Long id) {
        int count = relationMapper.deleteById(id);
        return count;
    }

    /**
     * 获取关联详情
     */
    @Override
    public SmsFlashPromotionProductRelation getItem(Long id) {
        SmsFlashPromotionProductRelation relation = relationMapper.selectById(id);
        return relation;
    }

    /**
     * 分页查询相关商品及促销信息
     *
     * @param flashPromotionId        限时购id
     * @param flashPromotionSessionId 限时购场次id
     */
    @Override
    public List<SmsFlashPromotionProduct> list(Long flashPromotionId, Long flashPromotionSessionId, Integer pageSize, Integer pageNum) {
        List<SmsFlashPromotionProduct> list = relationDao.getList(flashPromotionId, flashPromotionSessionId);

        return list;
    }
    
    @Override
    public Long getCount(Long flashPromotionId, Long flashPromotionSessionId) {
        QueryWrapper<SmsFlashPromotionProductRelation> wrapper = new QueryWrapper<>();
        wrapper.eq("flash_promotion_id",flashPromotionId)
                .eq("flash_promotion_session_id",flashPromotionSessionId);
        Long count = relationMapper.selectCount(wrapper);
        return count;
    }
}




